#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Library.Entity;
using Library.Services;
using System.Collections;
using System.Reflection;

#endregion

namespace Library
{
    public partial class UserControlUser : UserControl
    {
        UserService m_userService = new UserService();
        User m_current_user;
        public UserControlUser(string name, User current_user, MainForm mainForm)
        {
            InitializeComponent();
            this.Name = name;
            this.Text = name;
            labelTitle.Text = name;
            m_current_user = current_user; 
            initColumn();
            initData();
            initButtonClick();
            toolBarButtonDelete.Enabled = listView.SelectedItem != null;
        }

        private void initButtonClick()
        {
            toolBarButtonAddNew.Click += new EventHandler(toolBarButtonAddNew_Click);
            toolBarButtonDelete.Click += new EventHandler(toolBarButtonDelete_Click);
            listView.ColumnWidthChanged += new ColumnWidthChangedEventHandler(listView_ColumnWidthChanged);
            SettingService.ListViewSettingColumnWidthLoad(listView, this.Name);
        }

        void listView_ColumnWidthChanged(object sender, ColumnWidthChangedEventArgs e)
        {
            SettingService.ListViewSettingColumnWidthSave(listView, this.Name);
        }

        void toolBarButtonDelete_Click(object sender, EventArgs e)
        {
            if (listView.SelectedItem != null)
            {
                MessageBox.Show("Are you sure to delete?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, new EventHandler(DeleteData));
            }
        }

        void DeleteData(object sender, EventArgs e) 
        {
            try
            {
                MessageBoxWindow msg = (MessageBoxWindow)sender;
                if (msg.DialogResult == DialogResult.Yes)
                {
                    User user = (User)listView.SelectedItem.Tag;
                    if (user.Id > 0)
                    {
                        if (user.Code.ToLower() == "admin")
                            throw new Exception("Can not delete Admin user!");
                        m_userService.DeleteUser(user);
                        MessageBox.Show("Record has been deleted.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                        listView.Items.Remove(listView.SelectedItem);
                    }
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void toolBarButtonAddNew_Click(object sender, EventArgs e)
        {
            FormUser frm = new FormUser(new User(), m_current_user, this);
            frm.ShowDialog();
        }
       
        private void initColumn()
        {
            foreach (FieldInfo pi in (typeof(User)).GetFields())
            {
                if (!pi.Name.Contains("Id"))
                    if (!pi.Name.Contains("Password"))
                        if (!pi.Name.Contains("SESSION_ID"))
                            listView.Columns.Add(pi.Name, 75, HorizontalAlignment.Left);
            }
            //listView.Columns.Add("Code", 75, HorizontalAlignment.Left);
            //listView.Columns.Add("Name", 100, HorizontalAlignment.Left);
            //listView.Columns.Add("Active", 50, HorizontalAlignment.Left);
            //listView.Columns.Add("Remarks", 200, HorizontalAlignment.Left);
        }

        public void initData()
        {
            listView.Items.Clear();
            IList data = m_userService.GetAllUser();
            //foreach(User user in data)
            //{
            //    ListViewItem item = listView.Items.Add(user.Code);
            //    item.SubItems.Add(user.Name);
            //    item.SubItems.Add(user.Active ? "Yes" : "No");
            //    item.SubItems.Add(user.Remark);
            //    item.Tag = user;
            //}

            foreach (User user in data)
            {
                ArrayList values = new ArrayList();
                foreach (FieldInfo pi in (typeof(User)).GetFields())
                {
                    if (!pi.Name.Contains("Id"))
                        if (!pi.Name.Contains("Password"))
                            if (!pi.Name.Contains("SESSION_ID"))
                            {
                                object v = user.GetType().GetField(pi.Name).GetValue(user);
                                values.Add(v == null ? "" : v.ToString());
                            }
                }
                string[] vals = (string[])values.ToArray(typeof(string));
                ListViewItem item = listView.Items.Add(user.Code);
                item.SubItems.Clear();
                item.SubItems.AddRange(vals);
                item.Tag = user;
            }
        }

        //internal void AddNewData(User user)
        //{
        //    ListViewItem item = listView.Items.Add(user.Code);
        //    item.SubItems.Add(user.Name);
        //    item.SubItems.Add(user.Active ? "Yes" : "No");
        //    item.SubItems.Add(user.Remark);
        //    item.Tag = user;
        //}

        private void listView_DoubleClick(object sender, EventArgs e)
        {
            if (listView.SelectedItem != null)
            {
                User user = (User)listView.SelectedItem.Tag;
                FormUser frm = new FormUser(user, m_current_user, this);
                frm.ShowDialog();
            }
        }

        //internal void UpdateData(User user)
        //{
        //    listView.SelectedItem.SubItems[0].Text = user.Code;
        //    listView.SelectedItem.SubItems[1].Text = user.Name;
        //    listView.SelectedItem.SubItems[2].Text = user.Active ? "Yes" : "No";
        //    listView.SelectedItem.SubItems[3].Text = user.Remark;
        //    listView.SelectedItem.Tag = user;
        //}

        private void listView_SelectedIndexChanged(object sender, EventArgs e)
        {
            toolBarButtonDelete.Enabled = listView.SelectedItem != null;
        }
    }
}